package com.taptech.doufu.ui.view;

import java.util.HashMap;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

import com.taptech.doufu.util.ScreenUtil2;
import com.taptech.doufu.R;
import com.taptech.doufu.util.ImageManager;

/**
 * <p>
 * Company: DiaoBao
 * </p>
 *
 * @author huangzh
 * @date 2014-3-21
 */
public class NetworkImageView extends ImageView
{

    private boolean fullScreen = true;
    private boolean isRound = false;

    private final RectF roundRect = new RectF();
    private float rect_adius = 90;
    private final Paint maskPaint = new Paint();
    private final Paint zonePaint = new Paint();
    private float scale;

    public static HashMap<String, Bitmap> caches = new HashMap<String, Bitmap>();

    public NetworkImageView(Context context)
    {
        super(context);
        initLayoutParams();
    }

    public NetworkImageView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NetWorkImageView);
        fullScreen = a.getBoolean(R.styleable.NetWorkImageView_fullScreen, true);
        setMaxHeight(ScreenUtil2.SCREEN_PX_HEIGHT);
        a.recycle();
        initLayoutParams();
    }

    public NetworkImageView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NetWorkImageView);
        fullScreen = a.getBoolean(R.styleable.NetWorkImageView_fullScreen, false);
        setMaxHeight(ScreenUtil2.SCREEN_PX_HEIGHT);
        a.recycle();
        initLayoutParams();
    }

    public void setRectAdius(float adius)
    {
        rect_adius = adius;
        invalidate();
    }

    public float getRectAdius()
    {
        return rect_adius;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom)
    {
        super.onLayout(changed, left, top, right, bottom);
        if (isRound)
        {
            int w = getWidth();
            int h = getHeight();
            roundRect.set(0, 0, w, h);
        }

    }

    @Override
    public void draw(Canvas canvas)
    {
        try
        {
            if (isRound)
            {
                canvas.saveLayer(roundRect, zonePaint, Canvas.ALL_SAVE_FLAG);
                canvas.drawRoundRect(roundRect, rect_adius, rect_adius, zonePaint);
                canvas.saveLayer(roundRect, maskPaint, Canvas.ALL_SAVE_FLAG);
            }
            super.draw(canvas);
            canvas.restore();

        } catch (IllegalArgumentException e)
        {
        } catch (RuntimeException e)
        {
        }

    }

    private void initLayoutParams()
    {

        maskPaint.setAntiAlias(true);
        maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        //
        zonePaint.setAntiAlias(true);
        zonePaint.setColor(Color.WHITE);
        //
        float density = getResources().getDisplayMetrics().density;
        rect_adius = rect_adius * density;

        Object obj = getLayoutParams();
        if (obj instanceof RelativeLayout.LayoutParams)
        {
            NetworkImageView.this.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        } else if (obj instanceof LinearLayout.LayoutParams)
        {
            NetworkImageView.this.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        }
    }

    public void setURL(final String url)
    {
        try
        {
            if (!TextUtils.isEmpty(url))
                ImageManager.displayImage(this, url, 0);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }


    @Override
    public void setImageBitmap(Bitmap bm)
    {
        if (scale > 0)
        {
            bm = ImageUtil.cutImage(bm, scale);
            bm = ImageUtil.zoomBitmap(bm, ScreenUtil2.SCREEN_PX_WIDTH, ScreenUtil2.SCREEN_PX_WIDTH / 2);
        }
        super.setImageBitmap(bm);
        if (fullScreen && scale == 0)
        {
            fullScreen();
        }
    }

    private void fullScreen()
    {
        if (isFullScreen())
        {
            BitmapDrawable drawable = (BitmapDrawable) getDrawable();
            if (drawable.getIntrinsicWidth() < ScreenUtil2.SCREEN_DP_WIDTH)
            {
                float drawableWidth = ScreenUtil2.DENSITY * drawable.getIntrinsicWidth();
                float drawbaleHeight = ScreenUtil2.DENSITY * drawable.getIntrinsicHeight();
                float matrix = ScreenUtil2.SCREEN_PX_WIDTH / drawableWidth;
                int height = (int) (drawbaleHeight * matrix);
                Object obj = getLayoutParams();

                if (obj instanceof RelativeLayout.LayoutParams)
                {
                    NetworkImageView.this.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, height));
                } else if (obj instanceof LinearLayout.LayoutParams)
                {
                    NetworkImageView.this.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, height));
                }

            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        if (fullScreen)
        {
            setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
        } else
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }

    }

    public boolean isFullScreen()
    {
        return fullScreen;
    }

    public void setFullScreen(boolean fullScreen)
    {
        this.fullScreen = fullScreen;
    }

    public boolean isRound()
    {
        return isRound;
    }

    public void setRound(boolean isRound)
    {
        this.isRound = isRound;
    }

    public float getScale()
    {
        return scale;
    }

    public void setScale(float scale)
    {
        this.scale = scale;
    }

}
